﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using MSIACCSL2.Views.SalesOrder;
using WcfService1.DTO;

namespace MSIACCSL2.Views.BTBElse
{
    public partial class FrmAdd : Page
    {
        private List<TextBox> txtNoLog;
        private List<TextBox> txtNoBundle;
        private List<TextBox> txtItemName;
        private SearchItem si;
        private int i = 0;
        private TextBox txtCurrentItem;
        private BtbElseDetailDTO btbdetail;
       

        public BtbElseWrapViewModel WrapViewModel
        {
            get
            {
                return this.Resources["POW"] as BtbElseWrapViewModel;
            }
        }

        public BtbElseViewModel ViewModel
        {
            get
            {
                return this.DataContext as BtbElseViewModel;
            }
        }

        public FrmAdd()
        {
            InitializeComponent();
            txtNoLog = new List<TextBox>();
            txtNoBundle = new List<TextBox>();
            txtItemName = new List<TextBox>();

            this.Loaded += (x, y) =>
                               {
                                   (this.Resources["POW"] as BtbElseWrapViewModel).BtbElseViewModel =
                                        (BtbElseViewModel)this.DataContext;
                                   txtTglBtbElse.Focus();
                               };
            

            this.KeyDown += (x, y) =>
            {
                if (y.Key == Key.T && Keyboard.Modifiers == ModifierKeys.Control)
                    WrapViewModel.AddBtbElseDetail(null);
                else if (y.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                    WrapViewModel.DeleteBtbElseDetail(null);
                else if (y.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.SaveBtbElse(ViewModel.BtbElse);
                else if (y.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    ViewModel.Cancel(null);
            };
        }

       
  
        void txtNoLog_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                
            }
           
            Dispatcher.BeginInvoke(() =>
                                       {
                                           ViewModel.CekSerialNumberExist((sender as TextBox).Text);
                                       });

        }

   
        private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                txtCurrentItem = sender as TextBox;
                btbdetail = txtCurrentItem.DataContext as BtbElseDetailDTO;
                e.Handled = true;
                ViewModel.GetItemByName(((TextBox)sender).DataContext);
                si = new SearchItem();
                si.Closed += new EventHandler(si_Closed);
                si.DataContext = ViewModel;
                si.Show();
            }
        }

        void si_Closed(object sender, EventArgs e)
        {
            if (si.dg1.SelectedItem != null)
            {
                btbdetail.Item = si.dg1.SelectedItem as ItemDTO;
                btbdetail.SatuanUnit = btbdetail.SatuanUnits.FirstOrDefault(x => x.KodeSatuan == (si.dg1.SelectedItem as ItemDTO).Satuan.KodeSatuan);
                btbdetail.SatuanPack =
                    btbdetail.SatuanPacks.FirstOrDefault(x => x.KodeSatuan == (si.dg1.SelectedItem as ItemDTO).SatuanPack.KodeSatuan);
            }

            DataGrid1.Focus();
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }

        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void textBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }


        private void btnCloseStatus_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ViewModel.CloseStatus();
        }

        private void txtNoLog_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                ViewModel.CekSerialNumberExist((sender as TextBox).Text);
            });
        }

    }
}

